

var table;
var validator;
//列表
function showOrderList() {
	$.ajax({url:"order/listPage.do", type:"get", dataType:"html", success:function (data) {
		$("#mainForm").html(data);
		table=$("#orderTable").table({
			width:"100%", 
			form:{
				url:"comm/search.do", 
				id:"#orderForm", 
				type:"post", 
				pageNumberText:"#pageNum"
			}, 
			pageable:true,
			selectable:true, 
			colums:[
				{columnName:"订单号", name:"ord_num",  handle:"editOrder"}, 
				{columnName:"客户名称", name:"ord_custom",}, 
				{columnName:"联系电话", name:"ord_mobile"}, 
				{columnName:"收款状态", name:"ord_flag", formatter:function(obj)
				   {
				   var status=obj.ord_flag =='0'?'收款':'未收款';
				    return status;
				  /* if (parseInt(obj.ord_flag)==0)
              {
                return"收款";
              }
           else if (parseInt(obj.ord_flag)==1)
              {
                return"未收款"
              }
				   }
				   */
				   }
				 }, 
				{columnName:"派送状态", name:"ord_state", formatter:function(obj){
			      var sta=obj.ord_state =='0'?'派送':'未派送';
				    return sta;
				 /* if (parseInt(obj.ord_state)==0)
				    {
				      return"派送";
				    }
			  	else if (parseInt(obj.ord_state)==1)
				    {
				      return"未派送"
				    }*/
				 
				  }
				  }
				]
		});
		//新建按钮
		$("#createBtn").click(function(){
			editOrder();
		});
		//查询按钮
		$("#searchBtn").click(function(){
			$("#pageNum").val(1);
			table.refresh();
		});
		//删除按钮
		$("#deleteBtn").click(function(){
			batchDeleteWidthCallbackHis('OrderEntity','ord_status',table.refresh);
		});
		
		//打印按钮
		$("#PrintBtn").click(function(){
      print('OrderEntity',table.refresh);
    });
	}});
}
//打印
 function print(type,callback) 
  {
		var ids = "";
	  $(".ids").each(function () {
	    if (this.checked == true) {
	      ids= this.value ;
	    }
	  });
	  $("#msgDialog").html("<p>确定要删除这些记录吗？</p>");
    window.open("order/print.do?orderEntityDtls.ord_id="+ids,'NEWWINODW');
  }
  
//新增、编辑
var flag_button='0';

function editOrder(id){
	var param="";
	if(id){
		param+="?orderEntity.id="+id;
	}
	$.ajax({
		url:"order/edit.do"+param,
		dataType:"html",
		success:function(html){
			$("#commonDialog").html(html);
			$("#commonDialog").dialog({
				title:"编辑商品信息",
				width:800,
				height:400,
				modal:true,
				buttons:{
					"保存":function(){
					if(flag_button=='0'){
					   flag_button="1";
					   saveCommodity();
					}
					},
					"返回":function(){
						$(this).dialog("close");
					}
				}
			});
		}
	});
}
//客户实体获取
function changeCustoms(id){
		$.ajax({
		url:"order/changeCustoms.do?changeId="+id,
		type:"post",
		dataType:"json",
		success:function(data){
		//var temp=data.result[0].实体中的数据名
		//$('#***').val(temp);为某个字段赋值
		var temp=data.result[0].cus_name;
		$('#orderEntity_ord_mobile').val(data.result[0].cus_mobile);
    $('#orderEntity_ord_custom').val(temp);
    $('#orderEntity_ord_custom_id').val(data.result[0].id);
    $('#orderEntity_ord_people').val(data.result[0].cus_people);
    $('#orderEntity_ord_address').val(data.result[0].cus_address);
		}
	});
}
//商品实体获取
function changeCommodity(obj){
		$.ajax({
		url:"order/changeCommodity.do?changeCoid="+obj.value,
		type:"post",
		dataType:"json",
		success:function(data){
  //var temp=data.result[0].实体中的数据名
    //$('#***').val(temp);为某个字段赋值
    var temp2=data.result[0].co_name;
    var s2=$(obj).parent().parent().find('.s2').get(0);
    s2=$(s2);
    s2.val(temp2);
    
    var s3=$(obj).parent().parent().find('.s3').get(0);
    s3=$(s3);
    s3.val(data.result[0].co_size);
    
    var s4=$(obj).parent().parent().find('.s4').get(0);
    s4=$(s4);
    s4.val(data.result[0].co_unit);
    
    var s5=$(obj).parent().parent().find('.s5').get(0);
    s5=$(s5);
    s5.val(data.result[0].co_num);
    
    var s6=$(obj).parent().parent().find('.s6').get(0);
    s6=$(s6);
    s6.val((data.result[0].co_price*1).toFixed(2));

    var s7=$(obj).parent().parent().find('.s7').get(0);
    s7=$(s7);
    s7.val(((s6.val()*1)*(s5.val()*1)).toFixed(2));
    
    var s8=$(obj).parent().parent().find('.s8').get(0);
    s8=$(s8);
    s8.val(data.result[0].co_remark);
   
		}
	   });
	}
	
	function count_price(obj){
	 var s5=$(obj).parent().parent().find('.s5').get(0);
	 s5=$(s5);
	 var s6=$(obj).parent().parent().find('.s6').get(0);
   s6=$(s6);
  //这里需要判断是否大于库存数量给出提示
    var s7=$(obj).parent().parent().find('.s7').get(0);
    s7=$(s7);
    s7.val(((s6.val()*1)*(s5.val()*1)).toFixed(2));
   
  
	}
 
 function saveCommodity()
{
  $("#orderEditForm").ajaxSubmit({
    url:"order/saveinfo.do",
    type:"post",
    dataType:"json",
    success:function(data){
      if(data.success){
        flag_button='0';
        $("#commonDialog").dialog("close");
        showDialog("保存成功");
        table.refresh();
      }
    }
  });
}